package org.jeecg.modules.demo.yxclosingdate.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.advancesReceived.entity.YxAdvancesReceived;
import org.jeecg.modules.demo.allout.entity.YxAllot;
import org.jeecg.modules.demo.caiWuSkd.entity.CwSkd;
import org.jeecg.modules.demo.caiWuSrOrder.entity.CwSrOrder;
import org.jeecg.modules.demo.checkOrder.entity.checkOrder;
import org.jeecg.modules.demo.cwSaleInvoice.entity.CwSaleInvoice;
import org.jeecg.modules.demo.puInvoice.entity.PuInvoice;
import org.jeecg.modules.demo.pubuyorder.entity.PuBuyOrder;
import org.jeecg.modules.demo.pusettlement.entity.PuSettlement;
import org.jeecg.modules.demo.qualityOrder.entity.PuQualityOrder;
import org.jeecg.modules.demo.soFeiyong.entity.SoFeiyong;
import org.jeecg.modules.demo.soOrderDetail.entity.SoOrderDetail;
import org.jeecg.modules.demo.soQualityOrder.entity.SoQualityOrder;
import org.jeecg.modules.demo.soSaleOrder.entity.SoSaleOrder;
import org.jeecg.modules.demo.soSettlement.entity.SoSettlement;
import org.jeecg.modules.demo.socost.entity.SoCost;
import org.jeecg.modules.demo.sopayorder.entity.SoPayorder;
import org.jeecg.modules.demo.utils.BigDecimalUtil;
import org.jeecg.modules.demo.utils.DateUtils;
import org.jeecg.modules.demo.utils.generateOddUtil;
import org.jeecg.modules.demo.warehouseorder.entity.WarehouseOrder;
import org.jeecg.modules.demo.yxadjust.entity.YxAdjust;
import org.jeecg.modules.demo.yxclosingdate.entity.YxClosingDate;
import org.jeecg.modules.demo.yxclosingdate.mapper.YxClosingDateMapper;
import org.jeecg.modules.demo.yxclosingdate.service.IYxClosingDateService;
import org.jeecg.modules.demo.yxclosingorder.entity.YxClosingOrder;
import org.jeecg.modules.demo.yxclosingorder.mapper.YxClosingOrderMapper;
import org.jeecg.modules.demo.yxkcqcorder.entity.YxKcqcOrder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: yx_closing_date
 * @Author: jeecg-boot
 * @Date:   2022-11-15
 * @Version: V1.0
 */
@Service
public class YxClosingDateServiceImpl extends ServiceImpl<YxClosingDateMapper, YxClosingDate> implements IYxClosingDateService {
    @Resource
    private YxClosingDateMapper yxClosingDateMapper;
    @Resource
    private YxClosingOrderMapper yxClosingOrderMapper;


    @Override
    public YxClosingDate getByDate(String str){
        return yxClosingDateMapper.getByDate(str);
    }

    @Override
    public List<YxClosingOrder> getAssociatedQueryByWoNo(String str){
        return yxClosingDateMapper.getAssociatedQueryByWoNo(str);
    }


    @Override
    public Integer getJzByDate( YxClosingDate yxClosingDate){
        return yxClosingDateMapper.getJzByDate(yxClosingDate);
    }
    @Override
    public String getNewstDate( YxClosingDate yxClosingDate){
        return yxClosingDateMapper.getNewstDate(yxClosingDate);
    }
    @Override
    public List<PuBuyOrder> getPuBuyOrder(YxClosingDate yxClosingDate){
        return yxClosingDateMapper.getPuBuyOrder(yxClosingDate);
    }
    @Override
    public List<PuQualityOrder> getPuQualityOrder(YxClosingDate yxClosingDate){
        return yxClosingDateMapper.getPuQualityOrder(yxClosingDate);
    }
    @Override
    public List<WarehouseOrder> getWarehouseOrder(YxClosingDate yxClosingDate){
        return yxClosingDateMapper.getWarehouseOrder(yxClosingDate);
    }
    @Override
    public List<PuSettlement> getPuSettlement(YxClosingDate yxClosingDate){
        return yxClosingDateMapper.getPuSettlement(yxClosingDate);
    }
    @Override
    public List<PuInvoice> getPuInvoice(YxClosingDate yxClosingDate){
        return yxClosingDateMapper.getPuInvoice(yxClosingDate);
    }
    @Override
    public List<SoPayorder> getSoPayOrder(YxClosingDate yxClosingDate){
        return yxClosingDateMapper.getSoPayOrder(yxClosingDate);
    }
    @Override
    public List<SoCost> getSoCost(YxClosingDate yxClosingDate){
        return  yxClosingDateMapper.getSoCost(yxClosingDate);
    }
    @Override
    public List<YxAdvancesReceived> getYfk(YxClosingDate yxClosingDate){
        return yxClosingDateMapper.getYfk(yxClosingDate);
    }
    @Override
    public List<checkOrder> getPd(YxClosingDate yxClosingDate){
        return yxClosingDateMapper.getPd(yxClosingDate);
    }
    @Override
    public List<YxAdjust> getAdjust(YxClosingDate yxClosingDate){
        return yxClosingDateMapper.getAdjust(yxClosingDate);
    }
    @Override
    public List<YxAllot> getAllot(YxClosingDate yxClosingDate){
        return yxClosingDateMapper.getAllot(yxClosingDate);
    }
    @Override
    public List<YxKcqcOrder> getQcOrder(YxClosingDate yxClosingDate){
        return yxClosingDateMapper.getQcOrder(yxClosingDate);
    }

    @Override
    public List<SoSaleOrder> getSoorder(YxClosingDate yxClosingDate) {
        return yxClosingDateMapper.getSoorder(yxClosingDate);
    }

    @Override
    public List<SoOrderDetail> getSod(YxClosingDate yxClosingDate) {
        return yxClosingDateMapper.getSod(yxClosingDate);
    }

    @Override
    public List<SoQualityOrder> getSoq(YxClosingDate yxClosingDate) {
        return yxClosingDateMapper.getSoq(yxClosingDate);
    }

    @Override
    public List<SoSettlement> getSos(YxClosingDate yxClosingDate) {
        return yxClosingDateMapper.getSos(yxClosingDate);
    }

    @Override
    public List<CwSkd> getSosk(YxClosingDate yxClosingDate) {
        return yxClosingDateMapper.getSosk(yxClosingDate);
    }

    @Override
    public List<CwSaleInvoice> getSocw(YxClosingDate yxClosingDate) {
        return yxClosingDateMapper.getSocw(yxClosingDate);
    }

    @Override
    public List<SoFeiyong> getSofy(YxClosingDate yxClosingDate) {
        return yxClosingDateMapper.getSofy(yxClosingDate);
    }

    @Override
    public List<CwSkd> getSoysk(YxClosingDate yxClosingDate) {
        return yxClosingDateMapper.getSoysk(yxClosingDate);
    }

    @Override
    public List<CwSrOrder> getSosr(YxClosingDate yxClosingDate) {
        return yxClosingDateMapper.getSosr(yxClosingDate);
    }

    @Override
    public List<Map<String,String>> getList(String str){
        try{
            List<Map<String,String>> list = yxClosingDateMapper.getList(str);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }


    @Override
    @Transactional
    public boolean monthEndClosing(YxClosingDate yxClosingDate){
        try{

            yxClosingDate.setIsClosing(1);
            yxClosingDateMapper.insert(yxClosingDate);
            List<WarehouseOrder> warehouseOrders = yxClosingDateMapper.getUninvoicedRkOrder(yxClosingDate);
            if(!warehouseOrders.isEmpty()){
                for (WarehouseOrder warehouseOrder : warehouseOrders) {
                    if(warehouseOrder.getRklb()!=1){
                        continue;
                    }else{
                        YxClosingOrder yxClosingOrder = getClosingOrderRed(warehouseOrder,yxClosingDate);
                        yxClosingOrderMapper.insert(yxClosingOrder);
                    }

                }
            }

            return true;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }



    public YxClosingOrder  getClosingOrderRed(WarehouseOrder warehouseOrder,YxClosingDate yxClosingDate) throws ParseException {
        Map<String,BigDecimal> fpmap = yxClosingDateMapper.getFpByWoNo(warehouseOrder.getWoNo());
        Double fpje = fpmap.get("FPJE").doubleValue();
        Double fpsl = fpmap.get("FPSL").doubleValue();
        String str = yxClosingOrderMapper.getMaxCode();
        Map<String,String> map = new HashMap<>();
        if(str==null){
            map.put("maxCode",null);
        }else{
            map.put("maxCode",str);
        }
        String name ="II_";
        String resultCode = generateOddUtil.createCode(map,name);
        YxClosingOrder closingOrder = new YxClosingOrder();
        closingOrder.setJzOrder(resultCode);
        closingOrder.setWoNo(warehouseOrder.getWoNo());
        closingOrder.setCinvname(warehouseOrder.getCinvname());
        closingOrder.setWhNum(warehouseOrder.getWhNum());
        closingOrder.setOrderType(1);
        closingOrder.setDjlx("红字回冲单");
        closingOrder.setChumdwmc(warehouseOrder.getChumdwmc());
        closingOrder.setMeasureunit(warehouseOrder.getMeasureunit());
        closingOrder.setYwlx(warehouseOrder.getYwlx());
        Map<String,BigDecimal> jcmap = yxClosingOrderMapper.getJcByWhAndCnameAndDate(closingOrder);
        Double jcje = jcmap.get("JCJE").doubleValue();
        Double jcsl = jcmap.get("JCSL").doubleValue();
        Double b= 0.0;
        Double sl = BigDecimalUtil.sub(warehouseOrder.getQoQuantity().doubleValue(),fpsl);
        Double je = BigDecimalUtil.sub(warehouseOrder.getHsje().doubleValue(),fpje);
        closingOrder.setSl(new BigDecimal(sl).negate());
        closingOrder.setHsje(new BigDecimal(je).negate());
        closingOrder.setHsdj(new BigDecimal(BigDecimalUtil.div(sl,je,7)));
        closingOrder.setJcje(new BigDecimal(BigDecimalUtil.sub(jcje, warehouseOrder.getHsje().doubleValue())));
        closingOrder.setJcsl(new BigDecimal(BigDecimalUtil.sub(jcsl, warehouseOrder.getQoQuantity().doubleValue())));
        if(closingOrder.getJcsl().compareTo(new BigDecimal(b))==0){
            closingOrder.setJcdj(new BigDecimal(0.0));
        }else{
            closingOrder.setJcdj(new BigDecimal(BigDecimalUtil.div(closingOrder.getJcsl().doubleValue(), closingOrder.getJcje().doubleValue(),7)));
        }
        String jztime = new SimpleDateFormat("yyyyMM").format(yxClosingDate.getJzTime());
        String fistDay = DateUtils.getPreMonth(jztime)+"01";
        Date date = new SimpleDateFormat("yyyyMMdd").parse(fistDay);
        closingOrder.setCreateTime(date);
        closingOrder.setJzTime(yxClosingDate.getJzTime());
        return closingOrder;
    }



    @Override
    @Transactional
    public boolean cancelMonthEndClosing(YxClosingDate yxClosingDate){
        try{
            List<YxClosingOrder> yxClosingOrders = yxClosingOrderMapper.getOrderByClosingDate(yxClosingDate);
            for (YxClosingOrder yxClosingOrder : yxClosingOrders) {
                yxClosingOrderMapper.deleteById(yxClosingOrder.getId());
            }
            yxClosingDateMapper.deleteById(yxClosingDate.getId());
            return true;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }
}
